package cn.circle.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.circle.commom.model.DataGrid;
import cn.circle.system.dao.ISysUserDao;
import cn.circle.system.model.SysUser;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.service.ISysUserService;
import cn.circle.system.util.HqlHelper;

@Repository
public class SysUserService implements ISysUserService{

	
	@Autowired
	public ISysUserDao sysUserDao;
	
	DataGrid dg = new DataGrid();
	
	@Override
	public DataGrid datagrid(PageHelper ph,SysUser sysUser) {
		// TODO Auto-generated method stub
		DataGrid dg = new DataGrid();
	/*	List<SysUser> l = sysUserDao.datagrid();*/
		// 用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();

		// 注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer("from SysUser t where 1=1 ");
		
		if (sysUser.getPhone() != null && !"".equals(sysUser.getPhone())) {
			params.put("phone", "%" + sysUser.getPhone() + "%");
			hql.append(" and t.phone like :phone");
		}
		/*hql = HqlHelper.orderHql(hql, ph); // 排序
*/		List<SysUser> l = sysUserDao.find(hql.toString(), params, ph.getPage(), ph.getRows());
		dg.setRows(l);
		dg.setTotal(sysUserDao.count("select count(*) " + hql, params));
		dg.setRows(l);
		dg.setTotal((long)l.size());
		return dg;
	}

	@Override
	public List<SysUser> getUser(String id) {
		// TODO Auto-generated method stub
		return sysUserDao.getUser(id);
	}

	@Override
	public boolean save(SysUser sysUser) {
		// TODO Auto-generated method stub
		sysUserDao.save(sysUser);
		return true;
	}

	@Override
	public boolean remove(String depId) {
		// TODO Auto-generated method stub
		sysUserDao.removeAll(depId);
		System.out.println("2");
		return false;
	}

	@Override
	public boolean delete(String id) {
		// TODO Auto-generated method stub
		sysUserDao.deleteAll(id);
		return false;
	}

	@Override
	public boolean update(SysUser c) {
		// TODO Auto-generated method stub
		sysUserDao.updataAll(c);
		System.out.println("saveOrUpdate");
		return true;
	}

}
